package com.huawei.hianalytics.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Process;
import android.text.TextUtils;
import com.huawei.hianalytics.g.b;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

public class f {

    static class a extends Exception {
        a(String str) {
            super(str);
        }
    }

    private static Object a(Class cls, String str, Class[] clsArr, Object[] objArr) {
        Object obj = null;
        if (cls == null) {
            throw new a("class is null in invokeStaticFun");
        }
        if (clsArr == null) {
            if (objArr != null) {
                throw new a("paramsType is null, but params is not null");
            }
        } else if (objArr == null) {
            throw new a("paramsType or params should be same");
        } else if (clsArr.length != objArr.length) {
            throw new a("paramsType len:" + clsArr.length + " should equal params.len:" + objArr.length);
        }
        try {
            try {
                obj = cls.getMethod(str, clsArr).invoke(null, objArr);
            } catch (IllegalAccessException e) {
                b.c("globalStreamUtil", "invokeStaticFun(): method invoke Exception!");
            } catch (IllegalArgumentException e2) {
                b.c("globalStreamUtil", "invokeStaticFun(): Illegal Argument!");
            } catch (InvocationTargetException e3) {
                b.c("globalStreamUtil", "invokeStaticFun(): Invocation Target Exception!");
            }
        } catch (NoSuchMethodException e4) {
            b.c("globalStreamUtil", "invokeStaticFun(): cls.getMethod(),No Such Method !");
        }
        return obj;
    }

    private static Object a(String str, String str2, Class[] clsArr, Object[] objArr) {
        try {
            return a(Class.forName(str), str2, clsArr, objArr);
        } catch (ClassNotFoundException e) {
            b.c("globalStreamUtil", "invokeStaticFun() Not found class!");
            return null;
        } catch (a e2) {
            b.c("globalStreamUtil", "invokeStaticFun(): Static function call Exception ");
            return null;
        }
    }

    public static String a() {
        Object a = a("com.huawei.android.os.SystemPropertiesEx", "ro.huawei.build.display.id", "");
        b.b("globalStreamUtil", "SystemPropertiesEx: get rom_ver: " + a);
        if (!TextUtils.isEmpty(a)) {
            return a;
        }
        String str = Build.DISPLAY;
        b.b("globalStreamUtil", "SystemProperties: get rom_ver: " + str);
        return str;
    }

    private static String a(int i, String str) {
        String str2 = "";
        switch (i) {
            case 1:
            case 2:
            case 4:
            case 7:
            case 11:
                return "2G";
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 14:
            case 15:
                return "3G";
            case 13:
                return "4G";
            default:
                return (str.equalsIgnoreCase("TD-SCDMA") || str.equalsIgnoreCase("WCDMA") || str.equalsIgnoreCase("CDMA2000")) ? "3G" : str;
        }
    }

    public static String a(Context context) {
        if (a(context, "android.permission.ACCESS_NETWORK_STATE")) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            String str = "";
            if (connectivityManager != null) {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                    if (activeNetworkInfo.getType() == 1) {
                        return "WIFI";
                    }
                    if (activeNetworkInfo.getType() == 0) {
                        str = activeNetworkInfo.getSubtypeName();
                        b.b("HiAnalytics/event", "Network getSubtypeName : " + str);
                        return a(activeNetworkInfo.getSubtype(), str);
                    } else if (activeNetworkInfo.getType() == 16) {
                        String str2 = "COMPANION_PROXY";
                        b.c("HiAnalytics/event", "type name = " + str2);
                        return str2;
                    }
                }
            }
            return str;
        }
        b.c("HiAnalytics/event", "not have network state phone permission!");
        return "";
    }

    public static String a(InputStream inputStream, int i) {
        a aVar = new a(i);
        byte[] bArr = new byte[i];
        while (true) {
            int read = inputStream.read(bArr);
            if (((long) read) == -1) {
                break;
            }
            aVar.a(bArr, read);
        }
        return aVar.a() == 0 ? "" : new String(aVar.b(), "UTF-8");
    }

    public static String a(String str, String str2) {
        return b(str, str2);
    }

    private static String a(String str, String str2, String str3) {
        Object a = a(str, "get", new Class[]{String.class, String.class}, new Object[]{str2, str3});
        return a != null ? (String) a : str3;
    }

    public static void a(int i, Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                b.c("globalStreamUtil", "closeQuietly(): Exception when closing the closeable!");
            }
        }
    }

    public static boolean a(Context context, String str) {
        return context != null && context.checkPermission(str, Process.myPid(), Process.myUid()) == 0;
    }

    public static String b(String str, String str2) {
        if (TextUtils.isEmpty(str)) {
            return str2;
        }
        CharSequence a = a("android.os.SystemProperties", str, str2);
        if (TextUtils.isEmpty(a)) {
            a = a("com.huawei.android.os.SystemPropertiesEx", str, str2);
        }
        return a;
    }
}
